﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using VideoBlog.Data.Entities;

namespace VideoBlog.Web.Models
{
    public class IndexViewModel
    {
        public PlaylistViewModel Latest { get; private set; }
        public PlaylistViewModel Popular { get; private set; }
        public PlaylistViewModel Featured { get; private set; }
        public Video NowPlaying { get; private set; }
        public Category Category { get; private set; }
        public string IntroductionText { get; private set; }
        public string IntroductionTitle { get; private set; }
        public Category FrontPageCategory { get; private set; }
        

        public IndexViewModel()
        {
            VideoBlogEntities2 db = new VideoBlogEntities2();
            Playlist p = db.Playlist.Include("Category").Where(o => o.FrontPageFeatured).First();
            if (!p.Video.IsLoaded)
                p.Video.Load();
            NowPlaying = p.Video.First();
            Category = p.Category;
            Populate(db.Video.Include("Playlist").OrderBy(o => o.VideoDateAdded).ToList(), 
                     db.Video.Include("Playlist").OrderByDescending(o => o.VideoViewCount).ToList(),
                     p,
                     NowPlaying);
            IntroductionText = "Welcome to Rati0nality.com, enjoy your stay.";
            IntroductionTitle = "Welcome";
        }

        public IndexViewModel(int videoID)
        {
            VideoBlogEntities2 db = new VideoBlogEntities2();
            NowPlaying = db.Video.Include("Playlist.Category").Where(o => o.VideoID == videoID).First();
            Playlist p = NowPlaying.Playlist;
            if (!p.Video.IsLoaded)
                p.Video.Load();
            Category = p.Category;

            Populate(db.Video.Include("Playlist").Where(o => o.Playlist.Category.CategoryID == Category.CategoryID).OrderBy(o => o.VideoDateAdded).ToList(),
                     db.Video.Include("Playlist").Where(o => o.Playlist.Category.CategoryID == Category.CategoryID).OrderByDescending(o => o.VideoViewCount).ToList(),
                     p,
                     NowPlaying);
            Featured.Image = null;
        }

        public IndexViewModel(string channelName)
        {
            VideoBlogEntities2 db = new VideoBlogEntities2();
            Category = db.Category.Include("Playlist").Where(o => o.CategorySlug == channelName).First();
            Playlist p = Category.Playlist.First();
            if (!p.Video.IsLoaded)
                p.Video.Load();
            NowPlaying = p.Video.First();
            Populate(db.Video.Where(o => o.Playlist.Category.CategoryID == Category.CategoryID).OrderBy(o => o.VideoDateAdded).ToList(),
                     db.Video.Where(o => o.Playlist.Category.CategoryID == Category.CategoryID).OrderByDescending(o => o.VideoViewCount).ToList(),
                     p,
                     NowPlaying);
            IntroductionText = string.Format("This is the section on {0}, enjoy your stay.",Category.CategoryName);
            IntroductionTitle = string.Format("Inside: <span class='title'>{0}</span>",Category.CategoryName);
            FrontPageCategory = Category;

        }

        private void Populate(List<Video> latest, List<Video> popular, Playlist p, Video nowPlaying)
        {
            Latest = new PlaylistViewModel("Latest videos", latest, null);
            Popular = new PlaylistViewModel("Popular videos", popular, null);

            Featured = new PlaylistViewModel(p.PlaylistTitle,
                                             p.Video.OrderBy(o => o.Order).ToList(), nowPlaying);

            Featured.Image = string.Format(@"{0}.jpg", p.PlaylistID.ToString());
        }

    }
}
